package pers.dawnyang.modular.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;
import pers.dawnyang.common.annotation.SP;
import pers.dawnyang.common.constant.Const;
import pers.dawnyang.common.domain.entity.SysOperLog;
import pers.dawnyang.modular.system.domain.request.operLog.OperLogReqR;
import pers.dawnyang.modular.system.domain.response.operLog.OperLogRes;
import pers.dawnyang.modular.system.mapper.SysOperLogMapper;
import pers.dawnyang.modular.system.service.OperLogService;

/**
 * 操作日志记录
 * 
 * @ClassName: OperLogServiceImpl
 * @author: dawn yang
 * @date: 2020-08-24 10:22:03
 * 
 */
@Slf4j
@Service
public class OperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements OperLogService {

    @Autowired
    private SysOperLogMapper sysOperLogMapper;

    @SP
    @Override
    public Page<OperLogRes> getPage(Page<OperLogRes> page, OperLogReqR req) {
        // SqlTools.montageSql(req);
        Page<OperLogRes> resPage = sysOperLogMapper.getPage(page, req);
        // DictTransUtil.transList(resPage.getRecords());
        return resPage;
    }

    @SP
    @Override
    public OperLogRes getDetail(String id) {
        OperLogRes operLogRes = sysOperLogMapper.getDetail(id);
        // DictTransUtil.trans(operLogRes);
        return operLogRes;
    }

    @Async
    @Override
    public void insertOperLog(SysOperLog operlog) {
        /*    System.out.println("准备同步记录:" + operlog.getOperUri());
        try {
            Thread.sleep(10000);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        System.out.println("同步开始");*/
        String operReqParam = operlog.getOperReqParam();
        // 使用正则表达式过滤敏感字段
        String newStr = "";
        String expStr = "";
        for (String str : Const.EXCLUDE_FILED) {
            expStr += str + "\\s*=\\s*\\S*|";
            expStr += str + "\\s*:\\s*\\S*|";
        }
        newStr = operReqParam.replaceAll(expStr.substring(0, expStr.length() - 1), "******,");
        operlog.setOperReqParam(newStr);
        sysOperLogMapper.insert(operlog);
        // System.out.println("同步结束");

    }

    public static void main(String[] args) {
        /*   PropertyPreFilters.MySimplePropertyPreFilter excludefilter = new PropertyPreFilters().addFilter();
        excludefilter.addExcludes(EXCLUDE_FILED);
        String params = JSONObject.toJSONString(parameterMap, excludefilter);*/

        String targetStr = "req: EditPwdReq( oldPwd =  222222, newPwd=222222, confirmPwd=222222)";
        String newStr = "";
        String replace1 = "";
        String expStr = "";
        for (String str : Const.EXCLUDE_FILED) {

            expStr += str + "\\s*=\\s*\\S*|";
            expStr += str + "\\s*:\\s*\\S*|";
            // String formExp = str + "(\\s)*=(\\s\\S)*,";
            // String jsonExp = str + "\\s*:\\s*\\S*\\s*,";
        }
        System.out.println(expStr);
        newStr = targetStr.replaceAll(expStr.substring(0, expStr.length() - 1), "******,");
        System.out.println(targetStr);
        System.out.println(newStr);

        /*        String str1 = "广东省，福建省，北京市，海淀区，河北省，上海市";
        
        str1 = str1.replaceAll("(?:省|市|区|)", "");
        
        System.out.println("替换多个中文：" + str1);*/

        /*     String newStr2 =
            targetStr.replaceAll("oldPwd\\s*=\\s*\\S*|userPwd\\s*:\\s*\\S*|newPwd\\s*=\\s*\\S*", "oldPwd=******");
        // String newStr2 = targetStr.replaceAll("oldPwd(\\s)*=(\\s\\S)*,", "******");
        System.out.println(newStr2);*/

    }

}